<?php
namespace app\admin\controller;

use app\common\model\NoticeMessage as Message;
use app\common\model\NoticeLeave;
use app\common\model\QuestionType;
use app\common\model\Question;
use think\Db;
use think\Request;

class NoticeMessage extends Base
{

    function _initialize()
    {
        parent::_initialize();
        $this->assign('stamp',123);
    }

    /**
     * 通知消息
     * @return \think\response\View
     */
    public function index()
    {
        $where = [];
        if(!empty($_GET['title'])){
            $where['title'] = ['like','%'.trim(input('get.title')).'%'];
        }

        $rows = Message::getList($where);

        $this->assign([
            'title' => '通知消息',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    /**
     * 通知消息编辑
     * @return \think\response\View
     */
    public function edit()
    {
        if(request()->isPost()){
            $data  = input('post.');
            $model = new Message();

            $result = $model->saveChange($data);
            if($result){
                $this->success('操作成功',url('notice_message/index'));
            }
            $this->error($model->getError());
        }
        $this->_show('notice_message','id');
        $this->assign([
            'title' => '编辑',
        ]);
        return view();
    }

    public function change()
    {
        $data = input();
        $result = Db::name('users')->update(['status'=>$data['type'],'user_id'=>$data['id']]);
        if($result !== false){
            //改变用户状态后清空前端token
            cache('token_'.hashid($data['id']),null);
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    public function del(Request $request)
    {
        $request = $request->param();
        $result = Db::name($request['type'])->where('id',$request['id'])->delete();
        if ($result){
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }


    /**
     * 通知留言
     */
    public function leave()
    {
        $where['a.status'] = 1;
        if(!empty($_GET['nick_name'])){
            $where['u.nick_name'] = ['like','%'.trim(input('get.nick_name')).'%'];
        }
        $rows =  NoticeLeave::getList();
        $this->assign([
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }


    public function leave_edit(Request $request)
    {
        $user_id = $request->param('id');
        if (request()->isPost()){
            $data = input('post.');

            $result = NoticeLeave::create($data);
            if($result!==false){
                $this->success('保存成功','/notice_message/leave');
            }
            $this->error('操作失败');
        }

        $data['leave'] = NoticeLeave::where('user_id', $user_id)->order('id')->select();
        $data['user'] = \app\common\model\Users::where('user_id',$user_id)->field('nick_name,header_img,user_id')->find();
        $this->assign([
            'title' => '编辑',
            'data'  => $data
        ]);
        return view();

    }

    public function leave_del(Request $request)
    {
        $user_id = $request->param('id');
        $result = NoticeLeave::where('user_id', $user_id)->delete();

        if ($result)
            $this->success('删除成功');
        $this->error('删除失败');
    }
    
    
    /**
     * 问题类型
     */
    public function question_type()
    {
        $where = [];
        if(!empty($_GET['title'])){
            $where['title'] = ['like','%'.trim(input('get.title')).'%'];
        }
        $model = new QuestionType();
        $rows = $model->getList($where);
        $this->assign([
            'title' => '问题类型',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }
    
    public function type_edit()
    {
        if(request()->isPost()){
            $data  = input('post.');
            $model = new QuestionType();

            $result = $model->saveChange($data);
            if($result){
                $this->success('操作成功',url('notice_message/question_type'));
            }
            $this->error($model->getError());
        }
        $this->_show('question_type','id');
        $this->assign([
            'title' => '编辑',
        ]);
        return view();
    }
    
    /**
     * 问题列表
     */
    public function question()
    {
        $where = [];
        if(!empty($_GET['title'])){
            $where['title'] = ['like','%'.trim(input('get.title')).'%'];
        }
        if(!empty($_GET['question_type_id'])){
            $where['question_type_id'] = trim(input('get.question_type_id'));
        }
        $rows = Question::getList($where);
        $types = QuestionType::all();
        $this->assign([
            'rows' => $rows,
            'types' => $types,
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    public function question_edit()
    {
        if(request()->isPost()){
            $data  = input('post.');
            $model = new Question();
            $result = $model->saveChange($data);
            if($result){
                $this->success('操作成功',url('notice_message/question'));
            }
            $this->error($model->getError());
        }
        $this->_show('question','id');
        $types = QuestionType::all();
        $this->assign([
            'title' => '编辑',
            'types' => $types
        ]);
        return view();
    }

    /**
     * 发送极光推送
     */
    public function push()
    {
        if (request()->isPost()){
            $data = input('post.');
            $push_id = 'all';
            if (is_numeric($data['user_id'])){
                $push_id = Db::name('user_extend')->where('user_id',$data['user_id'])->value('j_push_id');
                if (empty($push_id)) $this->error('推送失败,用户无推送id');
            }
            $result = j_push($data['title'],$push_id);
            if ($result){
                $this->success('发布成功');
            }
        }
        $this->error('系统繁忙,请稍后重试');
    }

}
